package servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import DAO.BaseDAO;
import DTO.Employee;
import serviceImpl.VipDAO;
import util.UtilMD5;

@WebServlet(name="employee_zhuce",urlPatterns="/employee_zhuce")
public class VipRegisteServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private static final String Base_DIR = "F:\\超市管理系统设计\\employee_photo";

    public VipRegisteServlet() {
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag = null;
        System.out.println("1*1*1***1*1**");
        flag = request.getParameter("flag");
        switch(flag){
            //在login注册页面上的数据来进行注册
            case "emp_z":
                emp_z(request,response);
                break;
            //这个是在管理员后台页面获取到的值进行存储
            case "emp_a":
                emp_a(request,response);
                break;
            default:
                break;
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }



    protected  void emp_z(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        try {

            response.encodeRedirectURL("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");
            request.setCharacterEncoding("utf-8");
            SmartUpload su = new SmartUpload();//创建上传组件对象
            Date date = new Date();
            su.initialize(this, request, response);
            su.setAllowedFilesList("jpg,jpeg,png,gif");//设置允许上传的文件类型
            su.setMaxFileSize(1024*1024*2);//设置单个文件的最大大小
            su.upload();//开始上传
            String username = su.getRequest().getParameter("username").trim();
            String password = su.getRequest().getParameter("password").trim();//密码
//			String sex = new String(su.getRequest().getParameter("sex").trim().getBytes("GBK"),"utf-8");//性别
            String sex = "1";
//			String age = su.getRequest().getParameter("age").trim();//年龄
            int age = 18;
//			String education = new String(su.getRequest().getParameter("education").trim().getBytes("GBK"),"utf-8");//学历
            String education = "1";
            Files files = su.getFiles();//获取上传文件对象，可以看成是一个数组，但是并不是

            File file = files.getFile(0);//循环获取文件对象
            File file1 = files.getFile(1);//循环获取文件对象
            File file2 = files.getFile(2);//循环获取文件对象
            String fname =null;
            String fname1= null;
            String fname2 = null;
            if(file.getSize()>0){//判断文件大小
                String ext = file.getFileExt();//获取文件后缀
                fname = UUID.randomUUID() + "." + ext;//对文件进行重命名
                file.saveAs(Base_DIR + java.io.File.separator + fname);//按照指定的路径对文件进行存储
            }
            if(file1.getSize()>0){//判断文件大小
                String ext = file1.getFileExt();//获取文件后缀
                fname1 = UUID.randomUUID() + "." + ext;//对文件进行重命名
                file1.saveAs(Base_DIR + java.io.File.separator + fname1);//按照指定的路径对文件进行存储
            }
            if(file2.getSize()>0){//判断文件大小
                String ext = file2.getFileExt();//获取文件后缀
                fname2 = UUID.randomUUID() + "." + ext;//对文件进行重命名
                file2.saveAs(Base_DIR + java.io.File.separator + fname2);//按照指定的路径对文件进行存储
            }
            password = UtilMD5.MD5U(password);
            BaseDAO<Employee> ed = new VipDAO();
            Employee emp = new Employee();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy");
            String id = UUID.randomUUID().toString();
            emp.setId(id);
            emp.setName(username);
            emp.setPassword(password);
            emp.setSex(Integer.parseInt(sex));
            emp.setAge(age);
            emp.setEducation(Integer.parseInt(education));
            emp.setPhoto_fwp(fname);
            emp.setPhoto_ic(fname1);
            emp.setPhoto_ec(fname2);
            emp.setRegtime(sdf.format(date));
            emp.setDepid(0);
            emp.setSpecialty("无");
            String eid = sdf2.format(date).toString() + "" + sex + "" + age + "" + id.substring(0, 2) + education;
            emp.setEid(eid);
            String sql = "insert into employee(id,eid,name,password,sex,age,education,photo_fwp,photo_ic,photo_ec,regtime,depid,specialty) values"
                    + "(?,?,?,?,?,?,?,?,?,?,?,?,?)";
            try {
                if(ed.add(emp,sql)){
                    //持久化成功   重定向   注册成功之后进入登录页面
                    request.setAttribute("msg","注册成功");
                    request.getRequestDispatcher("login.jsp").forward(request, response);
                }else{
                    //持久化失败  提示注册失败，用户名已存在
                    request.setAttribute("error", "用户名已经存在");
                    request.getRequestDispatcher("employee_zhuce.jsp").forward(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
    }



    protected  void emp_a(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        try {
            response.encodeRedirectURL("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");
            request.setCharacterEncoding("utf-8");
            SmartUpload su = new SmartUpload();//创建上传组件对象
            Date date = new Date();
            su.initialize(this, request, response);
            su.setAllowedFilesList("jpg,jpeg,png,gif");//设置允许上传的文件类型
            su.setMaxFileSize(1024*1024*2);//设置单个文件的最大大小
            su.upload();//开始上传
            String username = su.getRequest().getParameter("username").trim();
            String sex = su.getRequest().getParameter("sex").trim();
            String age = su.getRequest().getParameter("age").trim();
            String job = su.getRequest().getParameter("job").trim();
            String condition = su.getRequest().getParameter("condition");
            String phone = su.getRequest().getParameter("phone");//电话号码
            Files files = su.getFiles();//获取上传文件对象，可以看成是一个数组，但是并不是

            File file = files.getFile(0);//循环获取文件对象
            File file1 = files.getFile(1);//循环获取文件对象
            String fname =null;
            String fname1= null;
            if(file.getSize()>0){//判断文件大小
                String ext = file.getFileExt();//获取文件后缀
                fname = UUID.randomUUID() + "." + ext;//对文件进行重命名
                System.out.println(Base_DIR);
                file.saveAs(Base_DIR + java.io.File.separator + fname);//按照指定的路径对文件进行存储
            }
            if(file1.getSize()>0){//判断文件大小
                String ext = file1.getFileExt();//获取文件后缀
                fname1 = UUID.randomUUID() + "." + ext;//对文件进行重命名
                file1.saveAs(Base_DIR + java.io.File.separator + fname1);//按照指定的路径对文件进行存储
            }
            BaseDAO<Employee> ed = new VipDAO();
            Employee emp = new Employee();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String id = UUID.randomUUID().toString();
            emp.setId(id);
            emp.setName(username);
            emp.setPhone(phone);
            emp.setPassword(UtilMD5.MD5U("123456"));
            emp.setSex(Integer.parseInt(sex));
            emp.setAge(Integer.parseInt(age));
            emp.setJob(job);
            emp.setCondition(Integer.parseInt(condition));
            emp.setPhoto_fwp(fname);
            emp.setPhoto_ic(fname1);
            emp.setRegtime(sdf.format(date));
            System.out.println(emp);
            String sql = "insert into employee(id,name,phone,password,sex,age,job,conditionn,photo_fwp,photo_ic,regtime) values"
                    + "(?,?,?,?,?,?,?,?,?,?,?)";
            try {
                if(ed.add(emp,sql)){
                    //持久化成功   重定向   注册成功之后进入登录页面
                    request.setAttribute("msg","注册成功");
                    request.getRequestDispatcher("vip_list.jsp").forward(request, response);
                }else{
                    //持久化失败  提示注册失败，用户名已存在
                    request.setAttribute("error", "用户名已经存在");
                    request.getRequestDispatcher("vip_list.jsp").forward(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
    }
}
